package com.freeLearning.boot.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.freeLearning.boot.common.annotation.Log;
import com.freeLearning.boot.common.enums.LogModuleEnum;
import com.freeLearning.boot.common.result.Result;
import com.freeLearning.boot.system.model.entity.School;
import com.freeLearning.boot.system.model.query.KeywordQuery;
import com.freeLearning.boot.system.model.query.SchoolPageQuery;
import com.freeLearning.boot.system.service.SchoolService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "15.学校接口")
@RestController
@RequestMapping("/api/v1/school")
@RequiredArgsConstructor
public class SchoolController {

    private final SchoolService schoolService;
    //查询所有学校
    @Operation(summary = "查询所有学校")
    @Log(value = "查询所有学校", module = LogModuleEnum.SCHOOL)
    @PostMapping("/all")
    public Result<List<School>> getAllSchools(@RequestBody KeywordQuery keywordQuery) {
        List<School> schools = schoolService.list(new QueryWrapper<School>().like("name",keywordQuery.getKeywords()));
        return Result.success(schools);
    }

    // 分页查询学校学校
    @Operation(summary = "分页查询学校学校")
    @PostMapping("/page")
    @Log(value = "分页查询学校学校", module = LogModuleEnum.SCHOOL)
    public Result<IPage<School>> pageQuerySchools(
            @RequestBody SchoolPageQuery schoolPageQuery
    ) {
        Page<School> page = new Page<>(schoolPageQuery.getPageNum(), schoolPageQuery.getPageSize());
        QueryWrapper<School> wrapper = new QueryWrapper<>();
        wrapper
                .like(schoolPageQuery.getName() != null, "name", schoolPageQuery.getName())
                .eq(schoolPageQuery.getLevel() != null,"level", schoolPageQuery.getLevel())
                .eq(schoolPageQuery.getCountry() != null, "country", schoolPageQuery.getCountry())
                .eq(schoolPageQuery.getProvince() != null, "province", schoolPageQuery.getProvince())
                .eq(schoolPageQuery.getCity() != null, "city", schoolPageQuery.getCity())
                .eq(schoolPageQuery.getDistrict() != null, "district", schoolPageQuery.getDistrict());

        IPage<School> schoolPage = schoolService.page(page,wrapper);
        return Result.success(schoolPage);
    }

    // 根据ID获取学校
    @GetMapping("/{id}")
    public Result<School> getSchoolById(@PathVariable Long id) {
        School school = schoolService.getById(id);
        return Result.success(school);
    }

    // 添加学校
    @Operation(summary = "添加学校")
    @Log(value = "添加学校", module = LogModuleEnum.SCHOOL)
    @PostMapping
    public Result<Boolean> addSchool(@RequestBody School school) {
        boolean status = schoolService.save(school);
        return Result.success(status);
    }

    // 更新学校
    @Operation(summary = "更新学校")
    @Log(value = "更新学校", module = LogModuleEnum.SCHOOL)
    @PutMapping("/{id}")
    public Result<Boolean> updateSchool(@PathVariable Long id, @RequestBody School school) {
        school.setId(id);
        boolean status = schoolService.updateById(school);
        return Result.success(status);
    }

    // 删除学校
    @Operation(summary = "删除学校")
    @Log(value = "删除学校", module = LogModuleEnum.SCHOOL)
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteSchool(@PathVariable Long id) {
        boolean status = schoolService.removeById(id);
        return Result.success(status);
    }

}
